package se.study.io.cha;

import java.io.*;
import java.util.Arrays;

/**
 * @Author: tiger
 * @Date:2018/5/8
 * @Description:字符流处理
 */
public class CharStudy {
    public static void main(String[] args) {
//        fileReader();
        inputStreamReader();// 输入转换流
//        fileWriter();
        outputStreamWriter();// 输出转换流
    }

    private static void outputStreamWriter() {
        try {
            File file = new File("C:\\Users\\Administrator\\Desktop\\output_test.txt");
            if (!file.exists()) {
                file.createNewFile();
            }
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file, true));
            outputStreamWriter.write("test test");
            outputStreamWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /***
     * void write(int c):向外写出一个字符
     * void write(char[] buffer):向外写出多个字符 buffer
     * void write(char[] buffer,int off,int len):把 buffer 数组中从索引 off 开始到 len个长度的数据写出去
     * void write(String str):向外写出一个字符串
     *
     * 注意如果这里有一个 缓冲的概念，如果写入文件的数据没有达到缓冲的数组长度，那么数据是不会写入到文件中的
     * 解决办法：手动刷新缓冲区 flush()
     * 或者直接调用 close() 方法，这个方法会默认刷新缓冲区
     */
    private static void fileWriter() {
        try {
            File file = new File("C:\\Users\\Administrator\\Desktop\\output_test.txt");
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter writer = new FileWriter(file, true);
            writer.write("this is test!!!");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void inputStreamReader() {
        try {
            File file = new File("C:\\Users\\Administrator\\Desktop\\output.txt");
            // charsetName:指定字符集读取
            InputStreamReader inputStream = new InputStreamReader(new FileInputStream(file), "utf-8");

            char[] chars = new char[20];
            int read = inputStream.read(chars);
            System.out.println("读取字符数量：" + read);
            System.out.println("读取字符数组内容：" + Arrays.toString(chars));
            System.out.println("读取内容：" + new String(chars));
            inputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 字节输入流
     * int read():每次读取一个字符，读到最后返回 -1
     * int read(char[] buffer):将字符读进字符数组,返回结果为读取的字符数
     * int read(char[] buffer,int off,int len):将读取的字符存储进字符数组 buffer，返回结果为读取的字符数，从索引 off 开始，长度为 len
     */
    private static void fileReader() {
        try {
            File file = new File("C:\\Users\\Administrator\\Desktop\\output.txt");

            FileReader fileReader = new FileReader(file);
            char[] chars = new char[20];
            int read = fileReader.read(chars);
            System.out.println("读取字符数量：" + read);
            System.out.println("读取字符数组内容：" + Arrays.toString(chars));
            System.out.println("读取内容：" + new String(chars));
            fileReader.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
